package routefinder.controller;

import java.util.Calendar;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;

import routefinder.module.*;

public class RouteFinderMainController {
	public static void main(String[] args) {
		List<RouteStore> routeStores = new ArrayList<RouteStore>();
		routeStores.add(new RouteStoreAn());
		routeStores.add(new RouteStoreAilin());
		routeStores.add(new RouteStoreJiayuan());
		routeStores.add(new RouteStoreSeaIaw());
				
		Scanner in = new Scanner(System.in);
		System.out.println("Java Route Store");
		while (true) 
		{
			System.out.println("Enter command: ");
			String cmd = in.nextLine();
			String[] cmdInfos = cmd.split(" ");

			try 
			{
				if (cmdInfos[0].equalsIgnoreCase("exit")) 
				{
					System.out.println("Good bye!");
					break;
				}
				if (cmdInfos[0].equalsIgnoreCase("addlocation") && cmdInfos[1] != null) 
				{
					for (RouteStore store : routeStores) 
					{
						store.addLocation(cmdInfos[1]);
					}
					System.out.println("Location " + cmdInfos[1] + " added.");
				} 
				else if (cmdInfos[0].equalsIgnoreCase("addpath") && cmdInfos.length == 4) 
				{
					for (RouteStore store : routeStores)
					{
						store.addPath(cmdInfos[1], cmdInfos[2], Double.parseDouble(cmdInfos[3]));
					}
					System.out.println("Path from " + cmdInfos[1] + " to " + cmdInfos[2] 
					                    + " with distance " + cmdInfos[3] + " km added.");

				} 
				else if (cmdInfos[0].equalsIgnoreCase("distance") && cmdInfos.length == 3) 
				{
					double shortestDistance = -1;
					boolean samedistance = true;
					List<Double> searchResults = new ArrayList<Double>();
					long[] calculatingTime = new long[2];
					     
					for(RouteStore rs : routeStores)
					{
						long startTime = System.nanoTime();
						shortestDistance = rs.getShortestDistance(cmdInfos[1], cmdInfos[2]);
						searchResults.add(shortestDistance);
						long totalTime = System.nanoTime() - startTime;
						if (calculatingTime[0] == 0 || calculatingTime[0] > totalTime){
							calculatingTime[0] = totalTime;
						}
						if(calculatingTime[1] < totalTime){
							calculatingTime[1] = totalTime;
						}
					}
					
					for(Double distance : searchResults){
						if(shortestDistance != distance){
							samedistance = false;
							System.out.print("WARNING, values do not match: ");
							for(Double dist : searchResults){
								System.out.print(dist + ", ");
							}
							System.out.println();
							break;
						}
					}
					if(shortestDistance == -1 && samedistance){
						System.out.println("There is no route from " + cmdInfos[1] + " to " + cmdInfos[2]);
					}
					else if(samedistance)
					{
						System.out.println("Distance from " + cmdInfos[1] + " to " + cmdInfos[2] + 
								" is " + shortestDistance + " km.");
						System.out.println("Longest time for calculating: " + (calculatingTime[1]/1000000.0) + " ms");
						System.out.println("Shortest time for calculating: " + (calculatingTime[0]/1000000.0) + " ms");
					}
				} 
				else 
				{
					System.out.println("Incorrect command!");
				}
			} 
			catch (NumberFormatException ex) 
			{
				System.out.println("Incorrect command!");
			}
		}
	}
}
